package io.jnet.server.accepthandler.impl;

import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import io.jnet.common.api.ChannelContext;
import io.jnet.common.streamprocessor.ProcessorTask;
import io.jnet.common.util.AioListener;
import io.jnet.server.build.ServerChannelContextBuilder;
import io.jnet.server.build.ServerChannelContextConfig;
import io.jnet.server.serverchannelcontext.ConcentrationServerChannelContext;

public class ConcentrationAcceptHandler extends AbstractAcceptHandler
{
    private ExecutorService              executorService;
    private BlockingQueue<ProcessorTask> tasks;
    
    public ConcentrationAcceptHandler(ExecutorService executorService, BlockingQueue<ProcessorTask> tasks, AsynchronousServerSocketChannel serverSocketChannel, ServerChannelContextBuilder channelContextBuilder, AioListener serverListener)
    {
        super(serverSocketChannel, channelContextBuilder, serverListener);
        this.tasks = tasks;
        this.executorService = executorService;
    }
    
    @Override
    public void stop()
    {
        executorService.shutdown();
    }
    
    @Override
    protected ChannelContext parse(AsynchronousSocketChannel socketChannel, ServerChannelContextConfig config, AioListener listener)
    {
        ChannelContext serverChannelContext = new ConcentrationServerChannelContext(//
                tasks, //
                config.getBufStorage(), //
                config.getMaxMerge(), //
                listener, //
                config.getProcessors(), //
                socketChannel, //
                config.getFrameDecodec());
        return serverChannelContext;
    }
}
